package com.wyz.socket.core.impl.scheduler;

import com.wyz.socket.core.api.Scheduler;
import com.wyz.socket.core.utils.NameThreadFactory;

import java.io.IOException;
import java.util.concurrent.*;

public class SchedulerImpl implements Scheduler {

    private final ScheduledExecutorService service;
    private final ExecutorService single;

    public SchedulerImpl(int poolSize) {
        this.service = Executors.newScheduledThreadPool(poolSize, new NameThreadFactory("Scheduler-Thread-"));
        this.single = Executors.newSingleThreadExecutor(new NameThreadFactory("Single-Thread-"));
    }

    @Override
    public ScheduledFuture<?> scheduler(Runnable runnable, long delay, TimeUnit unit) {
        return service.schedule(runnable, delay, unit);
    }

    @Override
    public void execute(Runnable runnable) {
        this.single.execute(runnable);
    }

    @Override
    public void close() throws IOException {
        this.service.shutdownNow();
        this.single.shutdownNow();
    }
}
